package com.smsc.headend.connector;

import com.smsc.headend.connector.netty.NettyConfigHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.management.ManagementFactory;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@RestController
@EnableDiscoveryClient
@Slf4j
@EnableFeignClients
@ComponentScan(basePackages = {"com.smsc.headend.common","com.smsc.headend.connector"})
public class DeviceConnectorApplication {
    @RequestMapping("/execute")
    public String execute() {
        log.info("execute task at {} info {}", System.getenv("COMPUTERNAME"), ManagementFactory.getRuntimeMXBean().getName());
        return null;
    }

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            NettyConfigHelper.shutdown();
        }));
        new SpringApplicationBuilder(DeviceConnectorApplication.class).web(WebApplicationType.SERVLET).run(args);
    }
}

/*
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    private P2pMeterClientManager manager;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        manager.start();
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                manager.stop();
            }
        });
    }
}
*/
